package com.cy.store.service.impl;

import com.cy.store.entity.Address;
import com.cy.store.mapper.AddressMapper;
import com.cy.store.service.IAddressService;
import com.cy.store.service.ex.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;

@Service
public class AddressServiceImpl implements IAddressService {
    @Autowired
    private AddressMapper addressMapper;

    @Value("${user.address.max-count}")
    private int maxCount;

    @Override
    public Integer countByUid(Integer uid) {
        return addressMapper.countAddressByUid(uid);
    }

    @Override
    public Integer addNewAddress(Integer uid, String username, Address address) {
        int count = addressMapper.countAddressByUid(uid);
        if (count >= maxCount){
            throw new AddressCountLimitException("收货地址已达最大值");
        }
        Integer moRen = count == 0?1:0;
        address.setIsDefault(moRen);
        address.setUid(uid);
        address.setModifiedUser(username);
        address.setCreatedUser(username);
        address.setCreatedTime(new Date());
        address.setModifiedTime(new Date());
        Integer rows = addressMapper.insertAddress(address);
        if (rows != 1){
            throw new InsertAddressException("插入时发生未知异常");
        }
        return rows;
    }

    @Override
    public void setDefault(Integer aid, Integer uid, String username) {
        Address result = addressMapper.findAddressByAid(aid);
        if (result == null) {
            throw new AddressNotFoundException("用户收货地址不存在");
        }
        if (result.getUid() != uid) {
            throw new AccessDeniedException("非法数据访问");
        }
        Integer rows = addressMapper.updateNoDefaultByUid(uid);
        if (rows < 1) {
            throw new UpdateDefaultAddressException("将用户收货地址全部设为非默认时发生异常");
        }
        rows = addressMapper.updateDefaultByAid(aid, username, new Date());
        if (rows != 1) {
            throw new UpdateException("修改默认地址时发生未知异常");
        }
    }

    @Override
    public void deleteAddressByAid(Integer aid,Integer uid,String username) {
        Address result = addressMapper.findAddressByAid(aid);
        if (result == null){
            throw new AddressNotFoundException("要删除的收获地址不存在");
        }
        if (result.getUid() != uid){
            throw new AccessDeniedException("非法数据访问");
        }
        Integer rows = addressMapper.deleteAddressByAid(aid);
        if (rows != 1){
            throw new DeleteException("删除时发生未知异常");
        }
        rows = addressMapper.countAddressByUid(uid);
        if (result.getIsDefault() == 1 || rows != 0){
            Address address = addressMapper.findLastUpdateAddressByUid(uid);
            rows = addressMapper.updateDefaultByAid(address.getAid(),username,new Date());
            if (rows != 1){
                throw new UpdateDefaultAddressException("设置新默认地址时发生异常");
            }

        }
    }
}
